package com.likerSai;

import java.util.ArrayList;
import java.util.List;

/**
 * z字形变换
 *
 * @author sun.ye
 * @since 2021-04-27 19:44
 */
public class Zconvert {
    public static void main(String[] args) {
        convert("AB",1);
    }
    public static String convert(String s, int numRows) {
        if (s.length() == 1){
            return s;
        }
        char[] chars = s.toCharArray();
        List<StringBuilder> convert = new ArrayList<>();
        for (int i = 0; i < numRows ; i++) {
            convert.add(new StringBuilder());
        }
        int cow = 0;
        int flag = 1;
        for (char aChar : chars) {
            convert.get(cow).append(aChar);
            cow += flag;
            if (cow == 0 || cow == numRows - 1) {
                flag *= -1;
            }
        }

        StringBuilder result = new StringBuilder();
        for (StringBuilder builder : convert) {
            result.append(builder);
        }
        return result.toString();
    }
}